use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use uuid::Uuid;
use wither::bson::{to_bson, Bson};
mod serde_default {
    use super::*;
    pub fn status_flow_type() -> ClubFormStatusFlowType {
        ClubFormStatusFlowType::Default
    }
    pub fn status_flow_primary_color() -> String {
        "#FFFFFF".to_string()
    }
    pub fn status_flow_default_success() -> bool {
        false
    }
    pub fn status_flow_default_failure() -> bool {
        false
    }
}

/// ## ClubFormStatusFlowType
/// 表单状态类型
/// TODO 使用类型来代替`is_default_success`和`is_default_failure`
#[derive(Debug, Clone, Copy, PartialOrd, PartialEq, Deserialize_repr, Serialize_repr)]
#[repr(u8)]
pub enum ClubFormStatusFlowType {
    /// 普通状态
    Default = 0,
    /// 草稿
    Draft = 1,
    /// 归档
    Archive = 2,
    /// 审核通过
    Success = 3,
    /// 审核未通过
    Failure = 4,
}
impl From<ClubFormStatusFlowType> for Bson {
    fn from(t: ClubFormStatusFlowType) -> Self {
        to_bson(&t).unwrap_or_default()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClubFormStatusFlow {
    pub id: String,
    pub title: String,
    pub description: Option<String>,
    #[serde(rename = "type", default = "serde_default::status_flow_type")]
    pub flow_type: ClubFormStatusFlowType,
    #[serde(default = "serde_default::status_flow_primary_color")]
    pub primary_color: String,
    #[serde(default = "serde_default::status_flow_default_success")]
    pub is_default_success: bool,
    #[serde(default = "serde_default::status_flow_default_failure")]
    pub is_default_failure: bool,
    #[serde(default = "Vec::new")]
    pub children: Vec<ClubFormStatusFlow>,
}
impl From<&ClubFormStatusFlow> for Bson {
    fn from(c: &ClubFormStatusFlow) -> Self {
        to_bson(c).unwrap_or_default()
    }
}

impl ClubFormStatusFlow {
    pub fn clone_without_children(&self) -> Self {
        Self {
            children: Vec::new(),
            ..self.clone()
        }
    }
    pub fn default_club_form_status_flow() -> ClubFormStatusFlow {
        ClubFormStatusFlow {
            id: Uuid::new_v4().to_string(),
            title: "等待审核".to_string(),
            description: None,
            flow_type: ClubFormStatusFlowType::Default,
            primary_color: serde_default::status_flow_primary_color(),
            is_default_success: false,
            is_default_failure: false,
            children: vec![
                ClubFormStatusFlow {
                    id: Uuid::new_v4().to_string(),
                    title: "审核未通过".to_string(),
                    description: None,
                    flow_type: ClubFormStatusFlowType::Failure,
                    primary_color: serde_default::status_flow_primary_color(),
                    is_default_success: false,
                    is_default_failure: true,
                    children: vec![],
                },
                ClubFormStatusFlow {
                    id: Uuid::new_v4().to_string(),
                    title: "审核通过".to_string(),
                    description: None,
                    flow_type: ClubFormStatusFlowType::Success,
                    primary_color: serde_default::status_flow_primary_color(),
                    is_default_failure: false,
                    is_default_success: true,
                    children: vec![],
                },
            ],
        }
    }
    pub fn default_draft_status<T: Into<Option<Vec<ClubFormStatusFlow>>>>(
        next_flow: T,
    ) -> ClubFormStatusFlow {
        let children: Option<Vec<ClubFormStatusFlow>> = next_flow.into();
        ClubFormStatusFlow {
            id: "draft".to_string(),
            title: "草稿".to_string(),
            description: None,
            flow_type: ClubFormStatusFlowType::Draft,
            primary_color: serde_default::status_flow_primary_color(),
            is_default_failure: false,
            is_default_success: false,
            children: children.unwrap_or_default(),
        }
    }
    pub fn default_archive_status() -> ClubFormStatusFlow {
        ClubFormStatusFlow {
            id: "archive".to_string(),
            title: "已归档".to_string(),
            description: None,
            flow_type: ClubFormStatusFlowType::Archive,
            primary_color: serde_default::status_flow_primary_color(),
            is_default_failure: false,
            is_default_success: true,
            children: vec![],
        }
    }
}
